//
//  MLSCellTopView.swift
//  MLSMircoBlog
//
//  Created by 袁航 on 15/8/2.
//  Copyright © 2015年 yuanhang. All rights reserved.
//

import UIKit
// 数字常量
private let headViewWidth: CGFloat = 35
private let headViewHeight: CGFloat = 35
private let nameLabelFontSize: CGFloat = 14
private let timeLabelFontSize: CGFloat = 9
private let sourceLabelFontSize: CGFloat = 9
class MLSCellTopView: UIView {

    // 模型数据
    var status: MLSUserStatus? {
        didSet{
            if let url = status?.user!.profileImageUrl {
                headImage.sd_setImageWithURL(url)
            }
            nameLabel.text = status?.user?.name ?? ""
            vipImageView.image = status?.user?.vipImage
            if let memberView = status?.user?.memberImage {
                memberImageView.image = memberView
                // 改变namelabel的颜色
                nameLabel.textColor = UIColor(red: 241/255.0, green: 60/255.0, blue: 0, alpha: 1)
            } else {
                memberImageView.image = nil
                nameLabel.textColor = UIColor.darkGrayColor()
            }
            timeLabel.text = "刚刚"
            sourceLabel.text = "weibo.com"
        }
    }
    
    // 初始化方法
    init(){
        super.init(frame: CGRectZero)
        backgroundColor = UIColor.clearColor()
        // 设置UI
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置布局
    private func setupUI(){
        addSubview(headImage)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(vipImageView)
        addSubview(memberImageView)
        
        // 自动布局
        headImage.ff_AlignInner(type: ff_AlignType.CenterLeft, referView: self, size: CGSize(width: headViewWidth, height: headViewHeight), offset: CGPoint(x: separatorMargin, y: separatorMargin))
        nameLabel.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: headImage, size: nil, offset: CGPoint(x: 2 * separatorMargin, y: 0))
        memberImageView.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: nameLabel, size: nil, offset: CGPoint(x: separatorMargin, y: 0))
        timeLabel.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: headImage, size: nil, offset: CGPoint(x: 2 * separatorMargin, y: 0))
        sourceLabel.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: timeLabel, size: nil, offset: CGPoint(x: separatorMargin, y: 0))
        vipImageView.ff_AlignInner(type: ff_AlignType.BottomRight, referView: headImage, size: nil, offset: CGPoint(x: vipImageView.frame.width * 0.5, y: vipImageView.frame.height * 0.5))
    }

    //MARK:- 懒加载控件
    private lazy var headImage:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var nameLabel = UILabel(color: UIColor.darkGrayColor(), fontSize: nameLabelFontSize)
    private lazy var timeLabel = UILabel(color: UIColor.orangeColor(), fontSize: timeLabelFontSize)
    private lazy var sourceLabel = UILabel(color: UIColor.lightGrayColor(), fontSize: sourceLabelFontSize)
    private lazy var vipImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_grassroot"))
    private lazy var memberImageView:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
}
